//
//  WillBeShownViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/6/30.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "WillBeShownViewController.h"
#import "RequestManager.h"
#import "WillBeShownModel.h"
#import <UIImageView+WebCache.h>
#import "WillBeShownView.h"
#import "WillBeShownDataViewController.h"
#import "WillBeShownCollectionViewController.h"
#import "WillBeShownCollectionViewCell.h"
#import "SlideImageView.h"



@interface WillBeShownViewController ()<RequestDelegate,SlideImageViewDelegate>

{
    MBProgressHUD *hud;
}

@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)NSMutableArray *array1;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)WillBeShownView *willBeShownView1;
@property(nonatomic,assign)int A;
@property(nonatomic,assign)BOOL B;

@end

@implementation WillBeShownViewController
-(void)dealloc{
    _array = nil;
    _array1 = nil;
    _collectionView = nil;
    _willBeShownView1 = nil;

    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.layer.contents = (id)[UIImage imageNamed:@"17478971_1358396642464.jpg"].CGImage;
    UIImage *image1 = [UIImage imageNamed:@"83-calendar.png"];
    UIImage *new = [image1 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:new style:(UIBarButtonItemStyleDone) target:self action:@selector(createcollectionView)];
    self.array = [NSMutableArray array];
    self.array1 = [NSMutableArray array];
    self.A = 0;

    

// 加载进度条
- (void)createdownLoadProgress{
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.color = [UIColor colorWithRed:0.616 green:0.617 blue:1.000 alpha:1.000];
    [self.view addSubview:hud];
    hud.labelText = @"T^T 请息怒，这个页面有点慢..";
    //设置模式为环状进度提示框
    hud.mode = MBProgressHUDModeIndeterminate;
    
    [hud show:YES];
    
    [hud release];
}

- (void)setLaout{
    self.view.layer.contents = (id)[UIImage imageNamed:@"17478971_1358396642464.jpg"].CGImage;
    
    UIImage *image1 = [UIImage imageNamed:@"83-calendar.png"];
    UIImage *new = [image1 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:new style:(UIBarButtonItemStyleDone) target:self action:@selector(createcollectionView)];

    
    [self createSlideImageView];


}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    self.array1 = [NSMutableArray array];
    self.A = 0;
    
    [self createdownLoadProgress];
    [self requestData];
    
    
    [self requestData];
    
}
#pragma mark------创建slideImageView--------------
-(void)createSlideImageView{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.willBeShownView1 = [[[WillBeShownView alloc] initWithFrame:self.view.bounds] autorelease];
    _willBeShownView1.slideImageView.delegate = self;
    [_willBeShownView1.button addTarget:self action:@selector(WillBeShownDataViewController) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_willBeShownView1];
    [_willBeShownView1.slideImageView reLoadUIview];
    [_willBeShownView1 reloadInputViews];
    
    
}
-(void)WillBeShownDataViewController{

    
    [self SlideImageViewDidClickWithIndex:_A];


}

- (void)SlideImageViewDidClickWithIndex:(int)index //对点击图片后的处理
{
    WillBeShownDataViewController *willBeShownDataVC = [[WillBeShownDataViewController alloc] init];
    willBeShownDataVC.str = _array1[index];
    [self.navigationController pushViewController:willBeShownDataVC animated:YES];
    
    [willBeShownDataVC release];
    
}
- (void)SlideImageViewDidScrollWithIndex:(int)index //滚动时的处理
{
    
    
    
}
- (void)SlideImageViewDidEndScorllWithIndex:(int)index //结束滚动后的处理
{
    _A = index;
    WillBeShownModel *model;
    model = _array[index];
    _willBeShownView1.pubdatelabel.text = model.pubdate;
    _willBeShownView1.titlelabel.text = model.title;
    _willBeShownView1.ratinglabel.text = [NSString stringWithFormat:@"推荐指数：%@",model.rating];
    
    
    
}
#pragma mark----------------创建collectionView-------------

-(void)createcollectionView{
    
//    [UIView transitionFromView:self.view toView: duration:1 options:(UIViewAnimationOptionTransitionFlipFromLeft) completion:^(BOOL finished) {
//        
//    }];//动画跳转

    
    WillBeShownCollectionViewController * willBeShownCollectionVC = [[WillBeShownCollectionViewController alloc] init];
    willBeShownCollectionVC.arrayCZ = _array;
    [self.navigationController pushViewController:willBeShownCollectionVC animated:NO];
    [willBeShownCollectionVC release];
   
}



#pragma mark -----------------网络请求-------------------------
-(void)requestData{
    RequestManager *request = [[RequestManager alloc] initWithRequestURL:@"http://api.douban.com/v2/movie/coming?apikey=0df993c66c0c636e29ecbb5344252a4a&client=e:iPhone4,1%7Cy:iPhone%20OS_6.1%7Cs:mobile%7Cf:doubanmovie_2%7Cv:3.3.1%7Cm:PP_market%7Cudid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&start=0&alt=json&version=2&app_name=doubanmovie" requestModel:GET parameter:nil];
    request.delegate = self;
    [request startRequest];



}
//请求完毕后让代理对象执行的方法，同时把请求完整的data传给代理对象
-(void)requestDidFinishWithData:(NSData *)data
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves) error:nil];
    NSArray *arr = [dic objectForKey:@"entries"];
    for (NSDictionary *dict in arr) {
        WillBeShownModel *model = [[WillBeShownModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [_array addObject:model];
        [_array1 addObject:model.ID];
        [model release];
    }
    
    
    [self createSlideImageView];
    [hud hide:YES];
    [self setLaout];
    
    _willBeShownView1.arrayCZ = _array;
    [_willBeShownView1 fzArray:_array];

    
}
//请求失败后吧错误原因传值给代理对象
-(void)requestError:(NSError *)error
{
    
    NSLog(@"%@",error);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
